com.sponsorpay.sdk.android.publisher.currency
Class VirtualCurrencyConnector

java.lang.Object
  extended by com.sponsorpay.sdk.android.publisher.currency.VirtualCurrencyConnector
All Implemented Interfaces:
SPCurrencyServerListener

public class VirtualCurrencyConnector
extends java.lang.Object
implements SPCurrencyServerListener

Provides services to access SponsorPay's Virtual Currency Server.


Nested Class Summary
private  class VirtualCurrencyConnector.CurrencyServerRequestAsyncTask
          VirtualCurrencyConnector.CurrencyServerRequestAsyncTask used to perform the HTTP requests on a background thread and be notified of its results on the calling thread.
static class VirtualCurrencyConnector.RequestType
          Types of requests to be sent to the Virtual Currency Server.
 
Field Summary
private static java.lang.String CURRENCY_DELTA_REQUEST_RESOURCE
           
private  Context mContext
          Android application context.
private  HostInfo mHostInfo
          HostInfo containing data about the host device and application, including its application ID.
private  java.lang.String mSecurityToken
          Security token used to sign requests to the server and verify its responses.
private  java.lang.String mUserId
          ID of the user for whom the requests will be made.
private  SPCurrencyServerListener mUserListener
          SPCurrencyServerListener registered by the developer code to be notified of the result of requests to the Virtual Currency Server.
private static java.lang.String STATE_LATEST_TRANSACTION_ID_KEY_PREFIX
          Key for the String containing the latest known transaction ID, which is saved as state in the Publisher SDK preferences file (whose name is defined in SponsorPayPublisher.PREFERENCES_FILENAME).
private static java.lang.String URL_PARAM_KEY_LAST_TRANSACTION_ID
           
private static java.lang.String URL_PARAM_KEY_TIMESTAMP
           
private static java.lang.String URL_PARAM_VALUE_NO_TRANSACTION
           
private static java.lang.String VIRTUAL_CURRENCY_SERVER_BASE_URL
           
 
Constructor Summary
VirtualCurrencyConnector(Context context, java.lang.String userId, SPCurrencyServerListener listener, HostInfo hostInfo, java.lang.String securityToken)
          Initializes a new instance with the provided context and application data.
 
Method Summary
 void fetchDeltaOfCoins()
          Sends a request to the SponsorPay currency server to obtain the variation in amount of virtual currency for a given user since the last time this method was called.
 void fetchDeltaOfCoinsForCurrentUserSinceTransactionId(java.lang.String transactionId)
          Sends a request to the SponsorPay currency server to obtain the variation in amount of virtual currency for the current user's transactions newer than the one whose ID is passed.
private  java.lang.String fetchLatestTransactionIdForCurrentUser()
          Retrieves the saved latest known transaction ID for the current user from the publisher state preferences file.
static java.lang.String fetchLatestTransactionIdForUser(Context context, java.lang.String userId)
          Retrieves the saved latest transaction ID for a given user from the publisher state preferences file.
private static java.lang.String getCurrentUnixTimestampAsString()
          Gets the current UNIX timestamp (in seconds) for the outbound requests.
private  void onCurrencyServerResponse(VirtualCurrencyConnector.CurrencyServerRequestAsyncTask requestTask)
          Called by VirtualCurrencyConnector.CurrencyServerRequestAsyncTask when a response from the currency server is received.
 void onSPCurrencyDeltaReceived(CurrencyServerDeltaOfCoinsResponse response)
          Implemented from SPCurrencyServerListener.
 void onSPCurrencyServerError(CurrencyServerAbstractResponse response)
          Implemented from SPCurrencyServerListener.
private  void saveLatestTransactionIdForCurrentUser(java.lang.String transactionId)
          Saves the provided transaction ID for the current user into the publisher state preferences file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VIRTUAL_CURRENCY_SERVER_BASE_URL

private static final java.lang.String VIRTUAL_CURRENCY_SERVER_BASE_URL
See Also:
Constant Field Values

CURRENCY_DELTA_REQUEST_RESOURCE

private static final java.lang.String CURRENCY_DELTA_REQUEST_RESOURCE
See Also:
Constant Field Values

URL_PARAM_KEY_LAST_TRANSACTION_ID

private static final java.lang.String URL_PARAM_KEY_LAST_TRANSACTION_ID
See Also:
Constant Field Values

URL_PARAM_VALUE_NO_TRANSACTION

private static final java.lang.String URL_PARAM_VALUE_NO_TRANSACTION
See Also:
Constant Field Values

URL_PARAM_KEY_TIMESTAMP

private static final java.lang.String URL_PARAM_KEY_TIMESTAMP
See Also:
Constant Field Values

STATE_LATEST_TRANSACTION_ID_KEY_PREFIX

private static final java.lang.String STATE_LATEST_TRANSACTION_ID_KEY_PREFIX
Key for the String containing the latest known transaction ID, which is saved as state in the Publisher SDK preferences file (whose name is defined in SponsorPayPublisher.PREFERENCES_FILENAME).

See Also:
Constant Field Values

mContext

private Context mContext
Android application context.


mUserId

private java.lang.String mUserId
ID of the user for whom the requests will be made.


mHostInfo

private HostInfo mHostInfo
HostInfo containing data about the host device and application, including its application ID.


mSecurityToken

private java.lang.String mSecurityToken
Security token used to sign requests to the server and verify its responses.


mUserListener

private SPCurrencyServerListener mUserListener
SPCurrencyServerListener registered by the developer code to be notified of the result of requests to the Virtual Currency Server.

Constructor Detail

VirtualCurrencyConnector

public VirtualCurrencyConnector(Context context,
                                java.lang.String userId,
                                SPCurrencyServerListener listener,
                                HostInfo hostInfo,
                                java.lang.String securityToken)
Initializes a new instance with the provided context and application data.

Parameters:
context - Android application context.
userId - ID of the user for whom the requests will be made.
listener - SPCurrencyServerListener registered by the developer code to be notified of the result of requests to the Virtual Currency Server.
hostInfo - HostInfo containing data about the host device and application, including its application ID.
securityToken - Security token used to sign requests to the server and verify its responses.
Method Detail

fetchDeltaOfCoins

public void fetchDeltaOfCoins()
Sends a request to the SponsorPay currency server to obtain the variation in amount of virtual currency for a given user since the last time this method was called. The response will be delivered to one of the registered listener's callback methods.


fetchDeltaOfCoinsForCurrentUserSinceTransactionId

public void fetchDeltaOfCoinsForCurrentUserSinceTransactionId(java.lang.String transactionId)
Sends a request to the SponsorPay currency server to obtain the variation in amount of virtual currency for the current user's transactions newer than the one whose ID is passed. The response will be delivered to one of the registered listener's callback methods.

Parameters:
transactionId - The transaction ID used as excluded lower limit to calculate the delta of coins.

onCurrencyServerResponse

private void onCurrencyServerResponse(VirtualCurrencyConnector.CurrencyServerRequestAsyncTask requestTask)
Called by VirtualCurrencyConnector.CurrencyServerRequestAsyncTask when a response from the currency server is received. Performs the first stage of error handling and initializes the right kind of CurrencyServerAbstractResponse.

Parameters:
requestTask - The calling VirtualCurrencyConnector.CurrencyServerRequestAsyncTask with the response data.

saveLatestTransactionIdForCurrentUser

private void saveLatestTransactionIdForCurrentUser(java.lang.String transactionId)
Saves the provided transaction ID for the current user into the publisher state preferences file. Used to save the latest transaction id as returned by the server.

Parameters:
transactionId - The transaction ID to save.

fetchLatestTransactionIdForCurrentUser

private java.lang.String fetchLatestTransactionIdForCurrentUser()
Retrieves the saved latest known transaction ID for the current user from the publisher state preferences file.

Returns:
The retrieved transaction ID or null.

fetchLatestTransactionIdForUser

public static java.lang.String fetchLatestTransactionIdForUser(Context context,
                                                               java.lang.String userId)
Retrieves the saved latest transaction ID for a given user from the publisher state preferences file.

Parameters:
context - Android application context.
userId - ID of the user related to which the fetched transaction ID must belong.
Returns:
The retrieved transaction ID or null.

getCurrentUnixTimestampAsString

private static java.lang.String getCurrentUnixTimestampAsString()
Gets the current UNIX timestamp (in seconds) for the outbound requests.

Returns:

onSPCurrencyServerError

public void onSPCurrencyServerError(CurrencyServerAbstractResponse response)
Implemented from SPCurrencyServerListener. Forwards the call to the user listener.

Specified by:
onSPCurrencyServerError in interface SPCurrencyServerListener
Parameters:
response - Instance implementing the CurrencyServerAbstractResponse.getErrorType(), CurrencyServerAbstractResponse.getErrorCode(), CurrencyServerAbstractResponse.getErrorMessage() and methods.

onSPCurrencyDeltaReceived

public void onSPCurrencyDeltaReceived(CurrencyServerDeltaOfCoinsResponse response)
Implemented from SPCurrencyServerListener. Saves the returned latest transaction id and forwards the call to the user listener.

Specified by:
onSPCurrencyDeltaReceived in interface SPCurrencyServerListener
Parameters:
response - A response instance that implements the CurrencyServerDeltaOfCoinsResponse.getDeltaOfCoins() and CurrencyServerDeltaOfCoinsResponse.getLatestTransactionId() methods.